package com.pac.analysis.controller;

import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import jxl.Workbook;
import jxl.format.Alignment;
import jxl.format.Border;
import jxl.format.BorderLineStyle;
import jxl.format.VerticalAlignment;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

import org.apache.commons.codec.CharEncoding;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.common.base.BaseController;
import com.common.util.SessionKeyContent;
import com.pac.analysis.bean.AbortionMonthF2Bean;
import com.pac.analysis.bean.HighChartDataBean;
import com.pac.analysis.bean.OptionProportionBean;
import com.pac.analysis.bean.UserMonthBean;
import com.pac.analysis.service.UserAnalysisService;
import com.pac.user.UserKeyContent;
import com.pac.user.bean.UserBean;

/**
 * 统计分析
 * 
 * @author lujun.chen
 * @version [版本号, 2015年12月30日]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
@Controller
@Scope("prototype")
@RequestMapping("/analysis")
public class AnalysisController extends BaseController {

	private static Logger logger = Logger.getLogger(AnalysisController.class);

	@Autowired
	private UserAnalysisService userAnalysisService;

	/**
	 * 统计每月人流数
	 * 
	 * @return
	 * @see [类、类#方法、类#成员]
	 */
	@RequestMapping("/user/month/count")
	@ResponseBody
	public List<UserMonthBean> countUserMonth(HttpServletRequest request,
			UserMonthBean userMonthBean) {

		HttpSession session = request.getSession();
		UserBean userBeans = (UserBean) session
				.getAttribute(SessionKeyContent.SESSION_KEY_OBJ_USER_BEAN);
		if (!UserKeyContent.USER_ID_SYSTEM.equals(userBeans.getId())) {
			userMonthBean.setDoctorUserId(userBeans.getId());
		}
		return userAnalysisService.countUserMonth(userMonthBean);
	}

	/**
	 * 每月流产人数统计
	 * 
	 * @param request
	 * @param abortionMonthF2Bean
	 * @return
	 * @see [类、类#方法、类#成员]
	 */
	@RequestMapping("/abortion/month/count")
	@ResponseBody
	public List<AbortionMonthF2Bean> countAbortionMonthF2(
			HttpServletRequest request, AbortionMonthF2Bean abortionMonthF2Bean) {
		HttpSession session = request.getSession();
		UserBean userBeans = (UserBean) session
				.getAttribute(SessionKeyContent.SESSION_KEY_OBJ_USER_BEAN);
		if (!UserKeyContent.USER_ID_SYSTEM.equals(userBeans.getId())) {
			abortionMonthF2Bean.setDoctorUserId(userBeans.getId());
		}
		return userAnalysisService.countAbortionMonthF2(abortionMonthF2Bean);
	}

	/**
	 * 随访统计
	 * 
	 * @param optionProportionBean
	 * @return
	 * @see [类、类#方法、类#成员]
	 */
	@RequestMapping("/follow/month/count")
	@ResponseBody
	public List<AbortionMonthF2Bean> countMonthFollow(
			HttpServletRequest request,
			OptionProportionBean optionProportionBean) {
		HttpSession session = request.getSession();
		UserBean userBeans = (UserBean) session
				.getAttribute(SessionKeyContent.SESSION_KEY_OBJ_USER_BEAN);
		if (!UserKeyContent.USER_ID_SYSTEM.equals(userBeans.getId())) {
			optionProportionBean.setDoctorUserId(userBeans.getId());
		}
		return userAnalysisService.countMonthFollow(optionProportionBean);
	}

	/**
	 * 流产原因
	 * 
	 * @param optionProportionBean
	 * @return
	 * @see [类、类#方法、类#成员]
	 */
	@RequestMapping("/abortion/reason/count")
	@ResponseBody
	public List<OptionProportionBean> countAbortionReasonF2(
			HttpServletRequest request,
			OptionProportionBean optionProportionBean) {
		HttpSession session = request.getSession();
		UserBean userBeans = (UserBean) session
				.getAttribute(SessionKeyContent.SESSION_KEY_OBJ_USER_BEAN);
		if (!UserKeyContent.USER_ID_SYSTEM.equals(userBeans.getId())) {
			optionProportionBean.setDoctorUserId(userBeans.getId());
		}
		return userAnalysisService.countAbortionReasonF2(optionProportionBean);
	}

	/**
	 * 统计人流总数
	 * 
	 * @param request
	 * @param abortionMonthF2Bean
	 * @return
	 * @see [类、类#方法、类#成员]
	 */
	@RequestMapping("/abortion/total/num")
	@ResponseBody
	public int abortionTotalNum(HttpServletRequest request,
			AbortionMonthF2Bean abortionMonthF2Bean) {
		HttpSession session = request.getSession();
		UserBean userBeans = (UserBean) session
				.getAttribute(SessionKeyContent.SESSION_KEY_OBJ_USER_BEAN);
		if (!UserKeyContent.USER_ID_SYSTEM.equals(userBeans.getId())) {
			abortionMonthF2Bean.setDoctorUserId(userBeans.getId());
		}

		return userAnalysisService.abortionTotalNum(abortionMonthF2Bean);
	}

	/**
	 * 成功随访统计
	 * 
	 * @param request
	 * @param optionProportionBean
	 * @return
	 * @see [类、类#方法、类#成员]
	 */
	@RequestMapping("/abortion/month/num")
	@ResponseBody
	public int abortionMonthNum(HttpServletRequest request,
			OptionProportionBean optionProportionBean) {
		HttpSession session = request.getSession();
		UserBean userBeans = (UserBean) session
				.getAttribute(SessionKeyContent.SESSION_KEY_OBJ_USER_BEAN);
		if (!UserKeyContent.USER_ID_SYSTEM.equals(userBeans.getId())) {
			optionProportionBean.setDoctorUserId(userBeans.getId());
		}
		return userAnalysisService.abortionMonthNum(optionProportionBean);
	}
	
	/**
	 * 导出Excel
	 * 
	 * @param request
	 * @return
	 * @see [类、类#方法、类#成员]
	 */
	@RequestMapping("/export/excel")
	@ResponseBody
	public void exportExcel(HttpServletResponse response,
			HighChartDataBean highChartDataBean) {

		try {
			OutputStream os = response.getOutputStream();
			response.reset();
			String fileName = highChartDataBean.getFileName();
			if (StringUtils.isEmpty(fileName)) {
				fileName = "报表";
			}
			fileName += ".xls";
			response.setHeader("Content-disposition", "attachment; filename="
					+ new String(fileName.getBytes(CharEncoding.UTF_8),
							CharEncoding.ISO_8859_1));

			response.setContentType("application/msexcel");
			WritableWorkbook workbook = Workbook.createWorkbook(os);
			WritableSheet sheet = workbook.createSheet("Sheet1", 0);

			jxl.SheetSettings sheetset = sheet.getSettings();
			sheetset.setProtected(false);

			WritableFont NormalFont = new WritableFont(WritableFont.ARIAL, 10);
			WritableCellFormat wcf_left = new WritableCellFormat(NormalFont);
			wcf_left.setBorder(Border.NONE, BorderLineStyle.THIN);
			wcf_left.setVerticalAlignment(VerticalAlignment.CENTRE);
			wcf_left.setAlignment(Alignment.LEFT);
			wcf_left.setWrap(false);

			int i = 0;
			int n = 0;
			List<String> titles = highChartDataBean.getTitle();
			for (String title : titles) {

				sheet.addCell(new Label(n, i, title, wcf_left));
				n++;
			}
			for (String line : highChartDataBean.getData()) {
				int j = 0;
				i++;
				String[] column = line.split("~");
				for (String cell : column) {
					sheet.addCell(new Label(j, i, cell, wcf_left));
					j++;
				}
			}
			workbook.write();
			workbook.close();

		} catch (IOException e) {
			logger.error("导出Excel出错，" + e.getMessage());
		} catch (RowsExceededException e) {
			logger.error("导出Excel出错，" + e.getMessage());
		} catch (WriteException e) {
			logger.error("导出Excel出错，" + e.getMessage());
		}
	}

}
